var express = require('express');
const Result = require('../lib/Result');
const isRule = require('../util/isRule');
const User = require('../db/models/user/user');
var router = express.Router();
/**
 * 用户列表,token需要21,173
*/
router.get("/user/:page", function (req, res) {
    let token = req.auth
    if (isRule(token.rule, [21, 173], res, token.super)) {
        let page=req.query.page
        let limit=req.query.limit
        let keyword=req.query.keyword
        let user_level_id=req.query.keyword
        User.getUserList(page,limit,keyword,user_level_id).then(value=>{
            res.json(Result.success(value))
        })
    }
})
/**
 * 增加用户,173
 */
router.post("/user",(req,res)=>{
    let token=req.auth
    if (isRule(token.rule,[173],res,token.super)) {
        User.addUser(req.body).then(value=>{
            res.json(Result.success(value.toJSON()))
        })
    }
})
/**
 * 修改用户状态,173
 */
router.post("/user/:id/update_status",(req,res)=>{
    let token=req.auth
    if (isRule(token.rule,[173],res,token.super)) {
        User.updateStatus(req.body.status,req.params.id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
/**
 * 删除用户,173
 */
router.post("/user/:id/delete",(req,res)=>{
    let token=req.auth
    if (isRule(token.rule,[173],res,token.super)) {
        User.deletebyId(req.params.id).then(value=>{
            res.json(Result.success(value))
        })
    }
})
/**
 * 修改用户,173
 */
router.post("/user/:id",(req,res)=>{
    let token=req.auth
    if (isRule(token.rule,[173],res,token.super)) {
        User.updateUser(req.body,req.params.id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
module.exports=router